package com.zlt.dao.Impl;

import com.zlt.dao.UserDao;
import com.zlt.entity.Role;
import com.zlt.entity.User;
import com.zlt.util.JDBCUtil;
import com.zlt.util.SqlUtil;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());

    public User selectUserByUid(String uid) {
        String sql = "select uid,username,password,nickname,reg_time,log_time,sex,salt,email,birthday,state,photo from user where uid=?";
        return SqlUtil.selectOne(User.class, sql, uid);
    }

//    public UUID getUserUid(String username) {}

    @Override
    public User selectUserByUsername(String phone) {
        String sql = "select uid,username,password,nickname,reg_time,log_time,sex,salt,email,birthday,state,photo from user where username=?";
        return SqlUtil.selectOne(User.class, sql, phone);
    }

    public String getUserUid(String username) {
        String sql = "SELECT uid FROM user WHERE username = ?";
        return template.queryForObject(sql, String.class, username);
    }

    @Override
    public int updateRecruit(User user) {
//        System.out.println(SqlUtil.update(user));
        String sql = "update user set nickname=?,sex=?,email=?,birthday=?,photo=? where username = ? ";
        return SqlUtil.update(sql, user.getNickname(), user.getSex(), user.getEmail(), user.getBirthday(), user.getPhoto(), user.getUsername());
    }


    @Override
    public int insert(User user) {
        return SqlUtil.insert(user);
    }

    @Override
    public User selectUserByUsername(String username, Integer state) {
        String sql = "select uid,username,password,nickname,reg_time,log_time,sex,salt,email,birthday,photo,state from user where username = ? and state = ?";
        return SqlUtil.selectOne(User.class, sql, username, state);
    }

    @Override
    public User selectUserByEmail(String email, Integer state) {
        String sql = "select uid,username,password,nickname,salt,email from user where email = ? and state = ?";
//        System.out.println("selectUserByUsername:" + SqlUtil.selectOne(User.class, sql, email, state));
        return SqlUtil.selectOne(User.class, sql, email, state);
    }

    @Override
    public List<Role> selectRolesByUid(String uid) {
        String sql = "select role.rid,role.name from user_role left join role on user_role.rid=role.rid where user_role.uid=?";
        System.out.println("selectRolesByUid:" + SqlUtil.select(Role.class, sql, uid));
        return SqlUtil.select(Role.class, sql, uid);
    }

    @Override
    public Integer getUserRole(String username) {
        String sql = "SELECT rid FROM user_role WHERE uid = (SELECT uid FROM user WHERE username = ?)";
        return template.queryForObject(sql, Integer.class, username);
    }
    @Override
    public List<Map<String, Object>> selectUserCompanyByUid(String uid) {
        String sql = "select ucid,uid,cid,state from user_company where uid=?";
        return SqlUtil.select(sql, uid);
    }

    @Override
    public int insertUserCompany(String uid, Integer cid, Integer state) {
        String sql = "insert into user_company (uid,cid,state) values (?,?,?)";
        return SqlUtil.update(sql, uid, cid, state);
    }

    @Override
    public int updateUserCompany(String uid, Integer cid, Integer state) {
        String sql = "update user_company set state = ? where uid =? and cid =?";
        return SqlUtil.update(sql, state, uid, cid);
    }

    @Override
    public int updateUser(User user) {
        String sql = "update user set password = ? where username = ?";
        return SqlUtil.update(sql, user.getPassword(), user.getUsername());
    }


    @Override
    public int updateUserByEmail(User user) {
        String sql = "update user set password = ? where email = ?";
        return SqlUtil.update(sql, user.getPassword(), user.getEmail());
    }


}
